package com.springboot.demo.controller;

import io.swagger.annotations.Api;
import org.apache.commons.collections.map.HashedMap;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @Description：
 * @Date： Created in 2022/1/6
 * @author： jianbin.lei
 * @Auto： <a href="mailto:493168771@qq.com">jianbin.lei</a>
 */
@Api(tags = {"8-Mail邮件管理","0-系统管理"})
@RestController
@RequestMapping(value = "/mail")
public class MailController {

    @Autowired
    private JavaMailSender mailSender;

    @GetMapping("/sendMail")
    public void sendMail(@RequestParam String message) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom("493168771@qq.com");
        simpleMailMessage.setTo("493168771@qq.com");
        simpleMailMessage.setSubject("主题：简单邮件测试");
        simpleMailMessage.setText(message);
        mailSender.send(simpleMailMessage);
    }

    @GetMapping("/sendMailByTp1")
    public void sendMailByTp1(@RequestParam String message) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom("493168771@qq.com");
            helper.setTo("493168771@qq.com");
            helper.setSubject("主题：模板邮件");

            Map<String, Object> model = new HashedMap();
            model.put("username", "嗨小叔");
            model.put("mailContent", message);
            //从spring1.4开始VelocityEngineUtils被弃用，spring4.3开始支持FreeMarker
//            String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "template.vm", "UTF-8", model);

            VelocityEngine velocityEngine = new VelocityEngine();
            Properties p = new Properties();
            p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "src/main/resources/templates");
            velocityEngine.init(p);
            VelocityContext velocityContext = new VelocityContext(model);
            StringWriter writer = new StringWriter();
            velocityEngine.mergeTemplate("mailTemplate1.vm", "UTF-8", velocityContext, writer);
            String text = writer.toString();
            helper.setText(text, true);
            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/sendMailByTp1AndRes")
    public void sendMailByTp1AndRes(@RequestParam String message) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom("493168771@qq.com");
            helper.setTo("493168771@qq.com");
            helper.setSubject("主题：模板邮件");

            Map<String, Object> model = new HashedMap();
            model.put("username", "嗨小叔");
            model.put("mailContent", message);
            //发送内容
            VelocityEngine velocityEngine = new VelocityEngine();
            Properties p = new Properties();
            p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "src/main/resources/templates");
            velocityEngine.init(p);
            VelocityContext velocityContext = new VelocityContext(model);
            StringWriter writer = new StringWriter();
            velocityEngine.mergeTemplate("mailTemplate1.vm", "UTF-8", velocityContext, writer);
            String text = writer.toString();
            helper.setText(text, true);
            //发送附件
            FileSystemResource file = new FileSystemResource(new File("src/main/resources/file/Image27.jpg"));
            helper.addAttachment("附件-1.jpg", file);

            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/sendMailByTp2AndResInline")
    public void sendMailByTp2AndResInline(@RequestParam String message) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom("493168771@qq.com");
            helper.setTo("493168771@qq.com");
            helper.setSubject("主题：模板邮件");

            Map<String, Object> model = new HashedMap();
            model.put("username", "嗨小叔");
            model.put("mailContent", message);
            //发送内容
            VelocityEngine velocityEngine = new VelocityEngine();
            Properties p = new Properties();
            p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "src/main/resources/templates");
            velocityEngine.init(p);
            VelocityContext velocityContext = new VelocityContext(model);
            StringWriter writer = new StringWriter();
            velocityEngine.mergeTemplate("mailTemplate2.vm", "UTF-8", velocityContext, writer);
            String text = writer.toString();
            helper.setText(text, true);
            //发送附件
            FileSystemResource file = new FileSystemResource(new File("src/main/resources/file/Image27.jpg"));
            helper.addAttachment("附件-1.jpg", file);
            //内容中嵌入静态资源文件
            FileSystemResource inlineFile = new FileSystemResource(new File("src/main/resources/file/Image27.jpg"));
            helper.addInline("image27",inlineFile);

            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

}
